<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios 发送 AJAX 请求</title>
    <!-- <script src="https://cdn.jsdelivr.net/npm/axios@1.1.2/dist/axios.min.js"></script> -->
    <!-- 上面的cdn连接在国外，访问较慢 -->
    <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.4/axios.js"></script>
    <!-- script标签本身具备跨域特性 -->
</head>
<body>
    <button>GET</button>
    <button>POST</button>
    <button>AJAX</button>

    <script>
        const btns = document.querySelectorAll('button');

        // 配置 baseURL
        axios.defaults.baseURL = 'http://127.0.0.1:8000';

        btns[0].onclick = function(){
            // GET 请求
            axios.get('/axios-server', {
                // url 参数
                params: {
                    id:200,
                    vip:7
                },
                // 请求头信息
                headers: {
                    name:'zane',
                    age:20
                }
            }).then(value => {
                console.log(value);
            }); // 获取数据较为完整
            // 数据返回与处理与jQuery不同，axios是基于promise，jQuery是用回调函数
        }

        btns[1].onclick = function(){
            axios.post('/axios-server', {
                // 请求体
                username:'admin',
                password:'admin'
            },{
                // url  行
                params: {
                    id:200,
                    zane:666
                },
                // 头
                headers: {
                    height:180,
                    weight:140
                }
            })
        }
    
        btns[2].onclick = function(){
            axios({
                method:'POST',
                // url
                url: '/axios-server',
                // url参数
                params: {
                    vip:10,
                    level:30
                },
                // 头信息
                headers: {
                    a:100,
                    b:200
                },
                // 请求体参数
                data: {
                    username:'zane',
                    password:'zane'
                }
            }).then(response=>{
                console.log(response);
                // 响应状态码
                console.log(response.status);
                // 响应状态字符串
                console.log(response.statusText);
                // 响应体
                console.log(response.data);
            })
        }
    </script>
</body>
</html>